package com.gopay.webtest.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.FundSignHandler;
import com.gopay.webtest.handler.citicSign.CiticSignUtils;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.util.MapUtil;
import com.gopay.webtest.vo.FundBalanceChangeNotifyParamVO;
import com.gopay.webtest.vo.citic.PageData;
import com.gopay.webtest.xml.XmlDao;

/**
 * 基金发送bis的控制类
 * @FundCiticController.java
 * @author dong-wang5@hnari.com
 * @2016年6月16日 下午4:01:43  www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/fundCitic")
@Controller
public class FundCiticController extends BaseController {
    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "fundSignHandler")
    private FundSignHandler fundSignHandler;

    /**
     * 监管行账户变动通知
     * @return
     */
    @RequestMapping("/balanceChangeNotify")
    public ModelAndView balanceChangeNotify() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_balanceChangeNotify", "balanceChangeNotify");
            mv = new ModelAndView("fund.balanceChangeNotify");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到监管行账户变动通知页面", e);
        }
        return mv;
    }

    @RequestMapping("/doBalanceChangeNotify")
    public ModelAndView doBalanceChangeNotify(@ModelAttribute FundBalanceChangeNotifyParamVO paramVo,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData(request);
        try {
            // 签名
            //fundSignHandler.sign(paramVo.getTranCode()+"_"+paramVo.getVersion(), "signType", "signValue", paramVo);
            pd.remove("submit");
            pd.remove("sign_type");
            String signBefore = MapUtil.sortStringByKey(pd, "&");
            //通过加签方式进行加签
            String signStra = CiticSignUtils.cartFundSignature(signBefore);
            mv.setViewName("fund.doBalanceChangeNotify");
            pd.put("sign_type", "RSA");
            pd.put("sign", signStra);
            mv.addObject("paramVo", pd);
        } catch (Exception e) {
            logger.error("执行监管行账户变动通知", e);
        }
        return mv;
    }

    /**
     * 基金公司文件上传通知
     * @return
     */
    @RequestMapping("/citicFileNotify")
    public ModelAndView citicFileNotify() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_citicFileNotify", "citicFileNotify");
            map.put("fileUploadTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            mv = new ModelAndView("fund.citicFileNotify");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到中信银行文件上传通知页面", e);
        }
        return mv;
    }

    @RequestMapping("/doCiticFileNotify")
    public ModelAndView doCiticFileNotify(HttpServletRequest request) {
        PageData pd = new PageData(request);
        ModelAndView mv = new ModelAndView();
        try {
            pd.remove("submit");
            pd.remove("sign_type");
            String signBefore = MapUtil.sortStringByKey(pd, "&");
            //通过加签方式进行加签
            String signStra = CiticSignUtils.cartFundSignature(signBefore);
            mv.setViewName("fund.doCiticFileNotify");
            pd.put("sign_type", "RSA");
            pd.put("sign",signStra);
            mv.addObject("paramVo", pd);
        } catch (Exception e) {
            logger.error("执行中信银行文件上传通知", e);
        }
        return mv;
    }

    /**
     * 监管行对账完成通知
     * @return
     */
    @RequestMapping("/citicReconFinishNotify")
    public ModelAndView citicReconFinishNotify() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("fund_citicReconFinishNotify", "citicReconFinishNotify");
            map.put("fileUploadTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            mv = new ModelAndView("fund.citicReconFinishNotify");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转到监管行对账完成通知页面", e);
        }
        return mv;
    }

    @RequestMapping("/doCiticReconFinishNotify")
    public ModelAndView doCiticReconFinishNotify(HttpServletRequest request) {
        PageData pd = new PageData(request);
        ModelAndView mv = new ModelAndView();
        try {
            pd.remove("submit");
            pd.remove("sign_type");
            String signBefore = MapUtil.sortStringByKey(pd, "&");
            //通过加签方式进行加签
            String signStra = CiticSignUtils.cartFundSignature(signBefore);
            mv.setViewName("fund.doCiticReconFinishNotify");
            pd.put("sign_type", "RSA");
            pd.put("sign",signStra);
            mv.addObject("paramVo", pd);
        } catch (Exception e) {
            logger.error("执行监管行对账完成通知", e);
        }
        return mv;
    }
}
